﻿/* Copyright (c) 2011 Mahir Iqbal
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.bayvakoof.dewall.geom
{
	import flash.geom.Point;
	
	/**
	 * Data structure for triangle faces
	 * @author Mahir Iqbal
	 */
	public class Face 
	{
		public var p1:Point;
		public var p2:Point;
		
		// Allows for fast lookup of whether this face
		// already belongs to a triangle and what point in the triangle is not part of this face
		//public var triPoint:Point;
		public var triPoint1:Point;
		
		public var triPoint2:Point;
		
		public var tri1:Triangle;
		
		public var tri2:Triangle;
		
		public var triSide1:int;
		
		public var triSide2:int;
		
		// Varibables used for calculations
		public var diffX:Number;
		public var diffY:Number;
		
		public var slope:Number;
		
		private var faceLength:Number;
		private var faceLengthSquared:Number;
		
		//public var startPoint:Point;
		
		//public var endPoint:Point;
		
		function Face(point1:Point, point2:Point)
		{
			/*if (point2.x > point1.x)
			{
				p2 = point1;
				p1 = point2;
			} else
			{
				p1 = point1;
				p2 = point2;
			}
			
			// Need these values pretty often.
			diffX = p2.x - p1.x;
			diffY = p2.y - p1.y;*/
			
			/*p1 = point1;
			p2 = point2;
			if(p2.x > p1.x)
			{
				diffX = p1.x - p2.x;
				diffY = p1.y - p2.y;
				startPoint = p2;
			}
			else
			{
				diffX = p2.x - p1.x;
				diffY = p2.y - p1.y;
				startPoint = p1;
			}*/
			p1 = point1;
			p2 = point2;
			
			// Need these values pretty often.
			diffX = p2.x - p1.x;
			diffY = p2.y - p1.y;
			
			slope = diffY / diffX;
			
			faceLength = 0;
			faceLengthSquared = 0;
		}
		
		public function equals(otherFace:Face):Boolean
		{
			return (p1.equals(otherFace.p1) && p2.equals(otherFace.p2)) || ((p1.x == p2.x) && p1.equals(otherFace.p2) && p2.equals(otherFace.p1));
		}
		
		public function get length():Number
		{
			if (faceLength != 0)
				return faceLength;
				
			faceLength = Point.distance(p1, p2);
			return faceLength;
		}
		
		public function get lengthSquared():Number
		{
			if (faceLengthSquared != 0)
				return faceLengthSquared;
				
			if (faceLength != 0)
				return faceLength * faceLength;
				
			var xDiff:Number = p1.x - p2.x;
			var yDiff:Number = p1.y - p2.y;
			return faceLengthSquared = xDiff * xDiff + yDiff * yDiff;
		}
		
		/**
		 * Returns the closest point on this Line to Point point (computed by the perpendicular).
		 * @param	point
		 * @return
		 */
		public function getClosestPoint(point:Point):Point
		{
			var u:Number = ((point.x - p1.x) * diffX + (point.y - p1.y) * diffY) / (diffX * diffX + diffY * diffY);
			return new Point(p1.x + u * diffX, p1.y + u * diffY);
			//var u:Number = ((point.x - startPoint.x) * diffX + (point.y - startPoint.y) * diffY) / (diffX * diffX + diffY * diffY);
			//return new Point(startPoint.x + u * diffX, startPoint.y + u * diffY);
		}		
		
		/**
		 * 
		 * @param	point
		 * @return
		 */
		public static const ABOVE:int = 1;  // In the case the slope of the line is infinite, this corresponds to LEFT
		public static const BELOW:int = -1; // whereas this is RIGHT
		public static const ON_LINE:int = 0;
		public function getSide(point:Point):int
		{
			var side:Number = diffX * (point.y - p1.y) - diffY * (point.x - p1.x);
			//var side:Number = diffX * (point.y - startPoint.y) - diffY * (point.x - startPoint.x);
			
			if (side == 0)
				return 0;
			else
				return (side > 0) ? 1 : -1;
		}
		
		public function getOppositedTriangle(tri:Triangle):Triangle
		{
			if(tri == tri1)
				return tri2;
			if(tri == tri2)
				return tri1;
			return null;
		}
		
		public function deleteTriangle(triangle:Triangle):void
		{
			if(tri1 == triangle)
			{
				tri1 = null;
				triPoint1 = null;
			}
			else if(tri2 == triangle)
			{
				tri2 = null;
				triPoint2 = null;
			}
		}
	}
	
}